package com.commerce.product.controller;




import com.commerce.common.entity.Product;
import com.commerce.common.entity.ProductImage;
import com.commerce.common.utils.PageResult;
import com.commerce.common.utils.Result;
import com.commerce.product.service.ProductImageService;
import com.commerce.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Autowired
    private ProductService productService;
    
    @Autowired
    private ProductImageService productImageService;

    /**
     * 分页查询商品列表
     */
    @GetMapping
    public Result<PageResult<Product>> getPageList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "12") int size,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long[] brandIds,
            @RequestParam(required = false) Integer minPrice,
            @RequestParam(required = false) Integer maxPrice,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer isHot,
            @RequestParam(required = false) Integer isNew,
            @RequestParam(defaultValue = "default") String sort) {
        PageResult<Product> pageResult = productService.getPageList(page, size, categoryId, brandIds, 
                                                                  minPrice, maxPrice, keyword, 
                                                                  isHot, isNew, sort);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取商品详情
     */
    @GetMapping("/{id}")
    public Result<Product> getProductById(@PathVariable Long id) {
        Product product = productService.getProductById(id);
        return Result.success(product);
    }

    /**
     * 批量获取商品信息
     */
    @GetMapping("/batch")
    public Result<List<Product>> getProductsByIds(@RequestParam("ids") List<Long> ids) {
        List<Product> products = productService.listByIds(ids);
        return Result.success(products);
    }

    /**
     * 添加商品
     */
    @PostMapping
    public Result<Long> addProduct(@RequestBody Product product) {
        Long productId = productService.addProduct(product);
        return Result.success(productId);
    }

    /**
     * 更新商品
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
        boolean success = productService.updateProduct(product);
        return Result.success(success);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProduct(@PathVariable Long id) {
        boolean success = productService.deleteProduct(id);
        return Result.success(success);
    }

    /**
     * 获取热销商品
     */
    @GetMapping("/hot")
    public Result<List<Product>> getHotProducts(@RequestParam(defaultValue = "10") int limit) {
        List<Product> products = productService.getHotProducts(limit);
        return Result.success(products);
    }

    /**
     * 获取新品
     */
    @GetMapping("/new")
    public Result<List<Product>> getNewProducts(@RequestParam(defaultValue = "10") int limit) {
        List<Product> products = productService.getNewProducts(limit);
        return Result.success(products);
    }

    /**
     * 获取推荐商品
     */
    @GetMapping("/recommend")
    public Result<List<Product>> getRecommendProducts(@RequestParam(defaultValue = "10") int limit) {
        List<Product> products = productService.getRecommendProducts(limit);
        return Result.success(products);
    }

    /**
     * 获取商品的所有图片
     */
    @GetMapping("/{id}/images")
    public Result<List<ProductImage>> getProductImages(@PathVariable Long id) {
        List<ProductImage> images = productImageService.getAllProductImages(id);
        return Result.success(images);
    }

    /**
     * 获取商品的特定类型图片
     * @param id 商品ID
     * @param type 图片类型：1-主图，2-详情图，3-规格图
     */
    @GetMapping("/{id}/images/{type}")
    public Result<List<ProductImage>> getProductImagesByType(@PathVariable Long id, @PathVariable Integer type) {
        List<ProductImage> images = productImageService.getProductImagesByType(id, type);
        return Result.success(images);
    }

    /**
     * 批量添加商品图片
     * @param id 商品ID
     * @param images 图片列表
     */
    @PostMapping("/{id}/images")
    public Result<Boolean> addProductImages(@PathVariable Long id, @RequestBody List<ProductImage> images) {
        // 设置商品ID
        images.forEach(image -> image.setProductId(id));
        boolean success = productImageService.saveProductImages(images);
        return Result.success(success);
    }
}